﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gaga.Core.Caching
{
	/// <summary>
	/// Extensions
	/// </summary>
	public static class CachingExtensions
	{
		/// <summary>
		/// Get a cached item. If it's not in the cache yet, then load and cache it
		/// </summary>
		/// <typeparam name="T">Type</typeparam>
		/// <param name="cacheManager">Cache manager</param>
		/// <param name="key">Cache key</param>
		/// <param name="acquire">Function to load item if it's not in the cache yet</param>
		/// <returns>Cached item</returns>
		public static T Get<T>(this ICacheManager cacheManager, string key, Func<T> acquire)
		{
			return Get(cacheManager, key, 60, acquire);
		}

		/// <summary>
		/// Get a cached item. If it's not in the cache yet, then load and cache it
		/// </summary>
		/// <typeparam name="T">Type</typeparam>
		/// <param name="cacheManager">Cache manager</param>
		/// <param name="key">Cache key</param>
		/// <param name="cacheTime">Cache time in minutes (0 - do not cache)</param>
		/// <param name="acquire">Function to load item if it's not in the cache yet</param>
		/// <returns>Cached item</returns>
		public static T Get<T>(this ICacheManager cacheManager, string key, int cacheTime, Func<T> acquire)
		{
			if (cacheManager.IsSet(key))
			{
				return cacheManager.Get<T>(key);
			}

			var result = acquire();
			if (cacheTime > 0)
				cacheManager.Set(key, result, cacheTime);
			return result;
		}
	}
}
